package com.baichuan.zntx.domain;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;

import java.util.HashMap;
import java.util.zip.CRC32;

/**
 * @author mazhanliang
 */
@Data
@JsonPropertyOrder({ "name", "id", "zitai", "time", "b_gps", "gpsStatus", "rfidStatus",
    "wakeUp", "trackStatus", "wheelStatus", "zitaiStatus", "batteryLevel" })
public class LoraMessage {
    @JsonProperty("Name")
    private String name;
    @JsonProperty("Id")
    private Integer id;
    @JsonProperty("Zitai")
    private String zitai;
    @JsonProperty("Time")
    private String time;
    @JsonProperty("B_GPS")
    private String b_gps;
    private Integer gpsStatus;
    private Integer rfidStatus;
    @JsonProperty("WakeUp")
    private Integer wakeUp;
    private Integer trackStatus;
    private Integer wheelStatus;
    private Integer batteryLevel;
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    private String crc32;
    private Integer zitaiStatus;

    @Override
    public String toString() {
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    public boolean crcCheck(){
        String jsonStr = this.toString();
        byte[] data = jsonStr.getBytes();
        CRC32 crc = new CRC32();
        crc.update(data);
        long crcValue = crc.getValue();
        String crcStr = Long.toHexString(crcValue);
        return this.crc32.equals(crcStr);
    }
}
